package org.rambadger.command.listeners;

import org.axonframework.eventhandling.annotation.EventHandler;
import org.axonframework.eventhandling.replay.ReplayAware;
import org.mongojack.DBUpdate;
import org.mongojack.JacksonDBCollection;
import org.rambadger.command.event.SubmissionCreated;
import org.rambadger.command.event.SubmissionPublished;
import org.rambadger.command.representation.Submission;

import static org.rambadger.command.representation.Submission.SubmissionId;

public class SubmissionListener implements ReplayAware{
    JacksonDBCollection<Submission, SubmissionId> submissions;

    public SubmissionListener(JacksonDBCollection<Submission, SubmissionId> submissions) {
        this.submissions = submissions;
    }
    @Override
    public void beforeReplay() {
        submissions.drop();
    }

    @Override
    public void afterReplay() {}

    @EventHandler void on(SubmissionCreated event) {
        submissions.insert(new Submission(event.getUsername(), event.getSubmissionId(), event.getPlatformId(), event.getAssignmentId()));
    }

}
